<!DOCTYPE html>
<html lang=zh>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>react开发日志 | 雨落</title>
  <meta name="description" content="安装 react + typescript 项目 不需要全局安装 create-react-app ，直接使用命令 npx create-react-app my-app --template typescript 安装  ts 知识点声明类型的方式 任意类型：any  let a:any &#x3D; 1 | &amp;#123;&amp;#125; | [] | &#39;1&#39;  类型别名：type（给类型起一">
<meta property="og:type" content="article">
<meta property="og:title" content="react开发日志">
<meta property="og:url" content="https://gitee.com/Yu-Luoboke/2021/12/14/react%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/index.html">
<meta property="og:site_name" content="雨落">
<meta property="og:description" content="安装 react + typescript 项目 不需要全局安装 create-react-app ，直接使用命令 npx create-react-app my-app --template typescript 安装  ts 知识点声明类型的方式 任意类型：any  let a:any &#x3D; 1 | &amp;#123;&amp;#125; | [] | &#39;1&#39;  类型别名：type（给类型起一">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-12-14T10:52:30.000Z">
<meta property="article:modified_time" content="2022-06-23T10:01:54.479Z">
<meta property="article:author" content="lianghongming">
<meta name="twitter:card" content="summary">
  <!-- Canonical links -->
  <link rel="canonical" href="https://gitee.com/Yu-Luoboke/2021/12/14/react%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/index.html">
  
    <link rel="alternate" href="/atom.xml" title="雨落" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/blog/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 5.2.0"></head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="http://yu-luoboke.gitee.io/blog" target="_blank">
          <img class="img-circle img-rotate" src="/blog/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">雨落</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">前端开发-奋斗青年</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> GuangZhou, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/blog/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="http://yu-luoboke.gitee.io/blog" target="_blank" title="Github" ><i class="icon icon-github"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Git/">Git</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/HTML-CSS/">HTML + CSS</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/JVM%EF%BC%88Java%E8%99%9A%E6%8B%9F%E6%9C%BA%EF%BC%89-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E7%AE%97%E6%B3%95/">JVM（Java虚拟机）-垃圾回收算法</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Node/">Node</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Project/">Project</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/React/">React</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/SVN/">SVN</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Vue-typescript/">Vue-typescript</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/gitee/">gitee</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/iOS/">iOS</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/js/">js</a><span class="category-list-count">12</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/vue/">vue</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/web-serve%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E6%9C%8D%E5%8A%A1%E5%99%A8/">web serve静态资源服务器</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/webpack/">webpack</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E5%89%8D%E7%AB%AF%E8%87%AA%E5%8A%A8%E5%8C%96/">前端自动化</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E5%B0%8F%E7%A8%8B%E5%BA%8F/">小程序</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E7%A7%BB%E5%8A%A8%E7%AB%AF/">移动端</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E7%BB%8F%E9%AA%8C/">经验</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a><span class="category-list-count">4</span></li></ul>
    </div>
  </div>


    
      

    
      
    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2024/04/">四月 2024</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/11/">十一月 2023</a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/09/">九月 2023</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/12/">十二月 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/08/">八月 2022</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/07/">七月 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/06/">六月 2022</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/04/">四月 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/01/">一月 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/12/">十二月 2021</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/11/">十一月 2021</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/10/">十月 2021</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/04/">四月 2021</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/03/">三月 2021</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/02/">二月 2021</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/01/">一月 2021</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/12/">十二月 2020</a><span class="archive-list-count">15</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/HTML-CSS/">HTML + CSS</a>
              </p>
              <p class="item-title">
                <a href="/blog/2024/04/15/%E6%8B%96%E5%8A%A8%E5%92%8C%E7%82%B9%E5%87%BB%E5%BC%B9%E7%AA%97/" class="title">拖动和点击弹窗</a>
              </p>
              <p class="item-date">
                <time datetime="2024-04-15T12:22:06.000Z" itemprop="datePublished">2024-04-15</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a>
              </p>
              <p class="item-title">
                <a href="/blog/2023/11/29/html%E9%9D%A2%E8%AF%95%E9%A2%98/" class="title">html面试题</a>
              </p>
              <p class="item-date">
                <time datetime="2023-11-29T15:58:27.000Z" itemprop="datePublished">2023-11-29</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a>
              </p>
              <p class="item-title">
                <a href="/blog/2023/11/29/css%E9%9D%A2%E8%AF%95%E9%A2%98/" class="title">css面试题</a>
              </p>
              <p class="item-date">
                <time datetime="2023-11-29T15:57:17.000Z" itemprop="datePublished">2023-11-29</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a>
              </p>
              <p class="item-title">
                <a href="/blog/2023/11/29/js%E9%9D%A2%E8%AF%95%E9%A2%98/" class="title">js面试题</a>
              </p>
              <p class="item-date">
                <time datetime="2023-11-29T15:37:30.000Z" itemprop="datePublished">2023-11-29</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a>
              </p>
              <p class="item-title">
                <a href="/blog/2023/11/29/%E9%9D%A2%E8%AF%95%E9%A2%98/" class="title">面试题</a>
              </p>
              <p class="item-date">
                <time datetime="2023-11-29T15:29:49.000Z" itemprop="datePublished">2023-11-29</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-react开发日志" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      react开发日志
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/blog/2021/12/14/react%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/" class="article-date">
	  <time datetime="2021-12-14T10:52:30.000Z" itemprop="datePublished">2021-12-14</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/blog/categories/React/">React</a>
  </span>

        

        

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/blog/2021/12/14/react%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/#comments" class="article-comment-link">评论</a></span>
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <h3 id="安装-react-typescript-项目"><a href="#安装-react-typescript-项目" class="headerlink" title="安装 react + typescript 项目"></a>安装 react + typescript 项目</h3><ul>
<li>不需要全局安装 create-react-app ，直接使用命令 <code>npx create-react-app my-app --template typescript</code> 安装</li>
</ul>
<h2 id="ts-知识点"><a href="#ts-知识点" class="headerlink" title="ts 知识点"></a>ts 知识点</h2><h3 id="声明类型的方式"><a href="#声明类型的方式" class="headerlink" title="声明类型的方式"></a>声明类型的方式</h3><ul>
<li><p>任意类型：any  <code>let a:any = 1 | &#123;&#125; | [] | &#39;1&#39;</code></p>
</li>
<li><p>类型别名：type（给类型起一个新的名字）</p>
<ul>
<li>联合类型：| : 表示该变量的值可以为这些类型 <code>type all = string | number | Array</code></li>
<li>交叉类型：&amp; : 表示该变量的值必须满足所有的数据类型（一般用于对象） <code>type obj = string &amp; number</code></li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 联合类型</span></span><br><span class="line"><span class="keyword">export</span> type all = string | number</span><br><span class="line"><span class="keyword">const</span> index: all = <span class="number">1</span> | <span class="string">&#x27;1&#x27;</span> <span class="comment">// 正确</span></span><br><span class="line">cosnt index: all = [] <span class="comment">// 报错，不能将数组类型分配给number</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 交叉类型：多用于对象</span></span><br><span class="line">interface obj &#123;</span><br><span class="line">    name: string</span><br><span class="line">    age: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface obj2 &#123;</span><br><span class="line">    sex: string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> type newObj = obj &amp; obj2</span><br><span class="line"></span><br><span class="line"><span class="comment">// 交叉类型中有三个属性，声明的对象中必须包含三个属性，切类型需要一致</span></span><br><span class="line"><span class="keyword">const</span> ojb = &#123;<span class="attr">name</span>: <span class="string">&#x27;Lisi&#x27;</span>, <span class="attr">age</span>: <span class="number">12</span>, <span class="attr">sex</span>: <span class="string">&#x27;nan&#x27;</span>&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li><p>可选属性：？可选属性只能读，不能赋值，如果一定要赋值，就能使用类型断言 as，前提是：一定得保证该属性中存在该属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在函数参数中使用，必须将可选参数放在参数的最后面</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fnc</span>(<span class="params">name: string, age?: number</span>) </span>&#123;&#125;</span><br><span class="line">interface obj &#123;</span><br><span class="line">    name: string </span><br><span class="line">    age?:number</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> demo = (param?: obj) &#123;</span><br><span class="line">    <span class="keyword">return</span> param?.age <span class="comment">// 因为 age 是可选参数，所以需要加上 ? 表示有值时才去访问 age ，无值时不去访问 age</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>取反：! 用在变量前表示取反，用在复制的内容后面时，使用 null 和 undefined 类型可以赋值给其它类型并通过编译</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> y:number</span><br><span class="line">y = <span class="literal">null</span>       <span class="comment">// 无法通过编译</span></span><br><span class="line">y = <span class="literal">undefined</span>  <span class="comment">// 无法通过编译</span></span><br><span class="line"></span><br><span class="line">y = <span class="literal">null</span>!</span><br><span class="line">y = <span class="literal">undefined</span>!</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 因为 x 属性是可选参数，因此 param.x 得到的类型为 number | undefined，当为 undefined 时无法传递给 number 类型的y，从而导致报错，因此需要使用 !</span></span><br><span class="line">interface demo &#123;</span><br><span class="line">    x?: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> y:number</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> demo = (param: demo) &#123;</span><br><span class="line">    y = param.x!</span><br><span class="line">    <span class="keyword">return</span> y</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<ul>
<li>类型断言：as 表示某属性中一定存在某属性</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">interface demo &#123;</span><br><span class="line">    x: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 编译报错，不能赋值给可选属性</span></span><br><span class="line"><span class="keyword">const</span> demo = <span class="function">(<span class="params">param?: demo</span>) =&gt;</span> &#123;</span><br><span class="line">    param?.x = <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果一定得赋值，可以使用类型断言，前提是一定得确保该属性中一定存在某属性</span></span><br><span class="line"><span class="keyword">const</span> demo = (param?: demo) &#123;</span><br><span class="line">    <span class="keyword">let</span> newValue = param <span class="keyword">as</span> demo</span><br><span class="line">    newValue.x = <span class="number">1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>定义数组类型</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr:number[] = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象数组</span></span><br><span class="line"><span class="keyword">const</span> obj: &#123; <span class="attr">name</span>: string &#125; [] = [&#123; <span class="attr">name</span>: <span class="string">&#x27;1&#x27;</span> &#125;]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 多种类型的数组，也叫泛型数组</span></span><br><span class="line"><span class="keyword">let</span> arr:<span class="built_in">Array</span>&lt;any&gt; = [<span class="number">1</span>,<span class="number">2</span>,<span class="string">&#x27;3&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象数组</span></span><br><span class="line">interface ArrObj &#123;</span><br><span class="line">    name: <span class="string">&#x27;李四&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> arr: <span class="built_in">Array</span>&lt;<span class="built_in">Object</span>&gt; = [&#123;&#125;, &#123;&#125;]</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(arr[<span class="number">0</span>].name) <span class="comment">// 李四    </span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 限定数组的个数和类型（元组：元组能向数组中添加属性，但是添加的属性不能使用，并且会报错）</span></span><br><span class="line"><span class="keyword">let</span> arr:[string, number] = [<span class="string">&#x27;1&#x27;</span>,<span class="number">2</span>] <span class="comment">// 数组长度只能等于2，类型需要一一对应</span></span><br><span class="line"><span class="keyword">let</span> arr:[string,number] = [<span class="string">&#x27;1&#x27;</span>,<span class="number">2</span>,<span class="number">3</span>] <span class="comment">// 错误：数组长度超了</span></span><br><span class="line"><span class="keyword">let</span> arr:[string,number] = [<span class="number">2</span>,<span class="number">3</span>] <span class="comment">// 错误：数组长度正常，但是类型没有一一对应</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 元组添加元素</span></span><br><span class="line">arr.push(<span class="number">2</span>) </span><br><span class="line"><span class="built_in">console</span>.log(arr) <span class="comment">// 结果是：[&#x27;1&#x27;, 2, 2]</span></span><br><span class="line">arr[<span class="number">2</span>] <span class="comment">// 不能使用，并且报错</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义数组，&lt;string&gt; 声明数组的类型（字符串数组：只能是字符串）</span></span><br><span class="line"><span class="keyword">let</span> arr:<span class="built_in">Array</span>&lt;string&gt; = [<span class="string">&#x27;1&#x27;</span>,<span class="string">&#x27;2&#x27;</span>,<span class="string">&#x27;3&#x27;</span>]</span><br></pre></td></tr></table></figure>

<ul>
<li>类</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    name: string;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">name: string</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name</span><br><span class="line">    &#125;</span><br><span class="line">    say () &#123;</span><br><span class="line">        consoleo.log(<span class="string">`my name is <span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span>`</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> lisi = <span class="keyword">new</span> Animal(<span class="string">&#x27;李四&#x27;</span>)</span><br><span class="line">lisi.say()</span><br></pre></td></tr></table></figure>

<ul>
<li>继承：可以使用父类方法，也能重写父类的属性方法</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    name: string;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">name: string</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name</span><br><span class="line">    &#125;</span><br><span class="line">    say () &#123;</span><br><span class="line">        consoleo.log(<span class="string">`my name is <span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span>`</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">newAnimal</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    eat () &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;eating&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> zhangsan = <span class="keyword">new</span> newAnimal(<span class="string">&#x27;张三&#x27;</span>)</span><br><span class="line">zhangsan.say()</span><br><span class="line">zhangsan.eat()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 重写方法：如果重写了父类的方法，需要使用父类方法时，需要在构造函数中调用 super 方法，并在后续中用 super 去执行父类方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    <span class="title">constructor</span> (<span class="params">name</span>) &#123;</span><br><span class="line">        <span class="built_in">super</span>(name)  <span class="comment">// 如果构造函数传入了参数，则 constructor 和 super 也应该传入参数</span></span><br><span class="line">    &#125;</span><br><span class="line">    say () &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;eating&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    animalSay () &#123;</span><br><span class="line">        <span class="built_in">super</span>.say()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> susi = <span class="keyword">new</span> Cat(<span class="string">&#x27;susi&#x27;</span>)</span><br><span class="line">susi.say()</span><br><span class="line">susi.animalSay()</span><br></pre></td></tr></table></figure>


<ul>
<li>泛型，约束函数返回值的类型</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在使用函数或类时，声明时不指定属性的类型，使用的时候再声明属性类型</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>&lt;<span class="title">T</span>&gt;(<span class="params">arg: T</span>): <span class="title">T</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> arg</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 根据实际传入的值的类型来判断返回类型</span></span><br><span class="line"><span class="keyword">const</span> result = func(<span class="literal">true</span>)  <span class="comment">// 函数返回类型是 boolean 类型</span></span><br><span class="line"><span class="keyword">const</span> result = func(<span class="string">&#x27;str&#x27;</span>) <span class="comment">// 函数返回类型是 string 类型</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果不使用泛型，函数可以匹配任意类型的数据，最终返回 any 类型</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params">arg</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> arg</span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">const</span> result = func(<span class="literal">true</span>)  <span class="comment">// any</span></span><br><span class="line"><span class="keyword">const</span> result = func(<span class="string">&#x27;str&#x27;</span>) <span class="comment">// any</span></span><br></pre></td></tr></table></figure>


<ul>
<li>约束泛型</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>&lt;<span class="title">T</span>&gt;(<span class="params">arg: T</span>): <span class="title">T</span> </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(arg.length) <span class="comment">// 由于不是所有的类型都具有 length 属性，所以会报错</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 所以需要结合继承来实现约束泛型（也是接口的鸭子类型特性的一种应用）</span></span><br><span class="line">interface properyLength &#123;</span><br><span class="line">    length: number</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>&lt;<span class="title">T</span> <span class="title">extends</span> <span class="title">properyLength</span>&gt;(<span class="params">arg: T</span>): <span class="title">T</span> </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(arg.length) <span class="comment">// 不会报错了</span></span><br><span class="line">    <span class="keyword">return</span> arg</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func(<span class="string">&#x27;你好呀&#x27;</span>) <span class="comment">// 3</span></span><br></pre></td></tr></table></figure>


<ul>
<li>接口 interface：自定义一个对象类型</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">interface Person &#123;</span><br><span class="line">    name: string</span><br><span class="line">    age: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个对象</span></span><br><span class="line"><span class="keyword">let</span> person: Person = &#123;</span><br><span class="line">    name: <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">    age: <span class="number">16</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// s声明一个对象数组：数组中每一个对象的类型是Person</span></span><br><span class="line"><span class="keyword">let</span> personArray: <span class="built_in">Array</span>&lt;person&gt; = [</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&#x27;lisit&#x27;</span>,</span><br><span class="line">        age: <span class="number">16</span></span><br><span class="line">    &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="ts-报错解放"><a href="#ts-报错解放" class="headerlink" title="ts 报错解放"></a>ts 报错解放</h2><h3 id="1、无法找到模块“react-jsx-runtime”的声明文件；react-demo-node-modules-react-jsx-runtime-js”隐式拥有-“any”-类型"><a href="#1、无法找到模块“react-jsx-runtime”的声明文件；react-demo-node-modules-react-jsx-runtime-js”隐式拥有-“any”-类型" class="headerlink" title="1、无法找到模块“react/jsx-runtime”的声明文件；react-demo/node_modules/react/jsx-runtime.js”隐式拥有 “any” 类型"></a>1、无法找到模块“react/jsx-runtime”的声明文件；react-demo/node_modules/react/jsx-runtime.js”隐式拥有 “any” 类型</h3><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 解决方法：在 tsconfig.json 文件中添加 &quot;noImplicitAny&quot;: false</span></span><br><span class="line">&#123;</span><br><span class="line"> <span class="attr">&quot;compilerOptions&quot;</span>: &#123;</span><br><span class="line">   <span class="attr">&quot;allowJs&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">   <span class="attr">&quot;allowSyntheticDefaultImports&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">   <span class="attr">&quot;esModuleInterop&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">   <span class="attr">&quot;isolatedModules&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">   <span class="attr">&quot;jsx&quot;</span>:<span class="string">&quot;react&quot;</span>,</span><br><span class="line">   <span class="attr">&quot;lib&quot;</span>: [<span class="string">&quot;es6&quot;</span>],</span><br><span class="line">   <span class="attr">&quot;moduleResolution&quot;</span>:<span class="string">&quot;node&quot;</span>,</span><br><span class="line">   <span class="attr">&quot;noEmit&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">   <span class="attr">&quot;strict&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">   <span class="attr">&quot;target&quot;</span>:<span class="string">&quot;esnext&quot;</span>,</span><br><span class="line">   <span class="attr">&quot;noImplicitAny&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">  &#125;,</span><br><span class="line"> <span class="attr">&quot;exclude&quot;</span>: [<span class="string">&quot;node_modules&quot;</span>,<span class="string">&quot;babel.config.js&quot;</span>,<span class="string">&quot;metro.config.js&quot;</span>,<span class="string">&quot;jest.config.js&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2、类型“Object”上不存在属性“xxxx”"><a href="#2、类型“Object”上不存在属性“xxxx”" class="headerlink" title="2、类型“Object”上不存在属性“xxxx”"></a>2、类型“Object”上不存在属性“xxxx”</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 报错代码</span></span><br><span class="line"><span class="comment">// config.ts</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> list: <span class="built_in">Object</span>[] = [</span><br><span class="line">    &#123;</span><br><span class="line">        id: <span class="string">&#x27;11,</span></span><br><span class="line"><span class="string">        title: &#x27;</span>标题<span class="string">&#x27;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">]</span></span><br><span class="line"><span class="string">// home.tsx</span></span><br><span class="line"><span class="string">import &#123;list&#125; from &#x27;</span>config.ts<span class="string">&#x27;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&lt;div&gt;</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">        list.map((item) =&gt; &#123;</span></span><br><span class="line"><span class="string">            return (</span></span><br><span class="line"><span class="string">                &lt;p key=&#123;item.id&#125;&gt;&#123;item.title&#125;&lt;/p&gt;</span></span><br><span class="line"><span class="string">            )</span></span><br><span class="line"><span class="string">        &#125;)</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&lt;/div&gt;</span></span><br><span class="line"><span class="string">// 此时：id 和 title 报错，提示 类型“Object”上不存在属性“id” ，类型“Object”上不存在属性“title”</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// 原因分析：遍历后的 item 是一个对象，但是 id 和 title 的类型没有声明，所以在 Object 上找不到</span></span><br><span class="line"><span class="string">// 解决方法：通过接口定义 id 和 title 的类型，修改 config.ts 文件，home.tsx 文件不需要修改</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// 通过接口声明 id 和 title 属性的类型</span></span><br><span class="line"><span class="string">interface obj &#123;</span></span><br><span class="line"><span class="string">    title: string</span></span><br><span class="line"><span class="string">    id: string</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// 引入接口声明的类型</span></span><br><span class="line"><span class="string">export const headerList: Array&lt;obj&gt; = [</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">        id: &#x27;</span><span class="number">11</span>,</span><br><span class="line">        title: <span class="string">&#x27;标题&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="在-tsx-文件中，html-标签报错，提示“无法使用-JSX，除非提供了-“–jsx”-标志”"><a href="#在-tsx-文件中，html-标签报错，提示“无法使用-JSX，除非提供了-“–jsx”-标志”" class="headerlink" title="在 tsx 文件中，html 标签报错，提示“无法使用 JSX，除非提供了 “–jsx” 标志”"></a>在 tsx 文件中，html 标签报错，提示“无法使用 JSX，除非提供了 “–jsx” 标志”</h3><ul>
<li><p>解决方法：将右下角的 “选择语言模式” 修改为typescript React 模式，如果还是不行，说明typescript的版本太低了，所以需要修改版本号，具体步骤如下：点击右下角 Go Live 右边的版本号，点击后顶部会有一个弹窗弹出，点击“选择typescript”版本号，然后再点击“使用工作区版本”即可</p>
</li>
<li><p>链接: <a target="_blank" rel="noopener" href="https://www.cnblogs.com/yezi1116/p/14120406.html"></a></p>
</li>
</ul>
<h2 id="功能"><a href="#功能" class="headerlink" title="功能"></a>功能</h2><h3 id="1、styled-components-给单个组件设置样式，不会对其它组件造成影响，安装插件-vscode-styled-components-可以实现代码高亮"><a href="#1、styled-components-给单个组件设置样式，不会对其它组件造成影响，安装插件-vscode-styled-components-可以实现代码高亮" class="headerlink" title="1、styled-components 给单个组件设置样式，不会对其它组件造成影响，安装插件 vscode-styled-components 可以实现代码高亮"></a>1、styled-components 给单个组件设置样式，不会对其它组件造成影响，安装插件 vscode-styled-components 可以实现代码高亮</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// home.js</span></span><br><span class="line"><span class="keyword">import</span> styled  <span class="keyword">from</span> <span class="string">&#x27;styled-components&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> html = styled.div<span class="string">`</span></span><br><span class="line"><span class="string">    width: 100px;</span></span><br><span class="line"><span class="string">    height: 100px;</span></span><br><span class="line"><span class="string">`</span></span><br><span class="line"><span class="comment">// home.tsx</span></span><br><span class="line"><span class="keyword">import</span> &#123;html&#125; <span class="keyword">from</span> <span class="string">&#x27;home.js&#x27;</span></span><br><span class="line"></span><br><span class="line">&lt;html&gt;&lt;<span class="regexp">/html&gt; /</span><span class="regexp">/ 渲染成div标签</span></span><br></pre></td></tr></table></figure>

<h3 id="2、防止-css-样式冲突"><a href="#2、防止-css-样式冲突" class="headerlink" title="2、防止 css 样式冲突"></a>2、防止 css 样式冲突</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第一中方法：使用 less 和 sass 的嵌套语法（推荐）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 第二种方法：使用 model</span></span><br><span class="line"><span class="comment">// 将文件命名为: home.module.css（默认导出文件，无需手动 export</span></span><br><span class="line">.title &#123;</span><br><span class="line">    color: red</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用</span></span><br><span class="line"><span class="keyword">import</span> home <span class="keyword">from</span> <span class="string">&#x27;home.module.css&#x27;</span></span><br><span class="line"></span><br><span class="line">&lt;p className=&#123;home.title&#125;&gt;文字&lt;/p&gt;</span><br></pre></td></tr></table></figure>

<h3 id="3、实现点击高亮（利用-useState-的值发生变化时会导致页面重新渲染的特性来实现高亮效果）"><a href="#3、实现点击高亮（利用-useState-的值发生变化时会导致页面重新渲染的特性来实现高亮效果）" class="headerlink" title="3、实现点击高亮（利用 useState 的值发生变化时会导致页面重新渲染的特性来实现高亮效果）"></a>3、实现点击高亮（利用 useState 的值发生变化时会导致页面重新渲染的特性来实现高亮效果）</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数组件中，使用 useState 来创建变量，保存组件的状态</span></span><br><span class="line"><span class="keyword">import</span> &#123; useState &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> demo = <span class="function">() =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> [state, setState] = useState(<span class="string">&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 当 useState 的值发生变化时，会重新渲染组件</span></span><br><span class="line">    <span class="keyword">const</span> activeClick = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        setState(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&#x27;1&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;div className=&#123;<span class="string">`box <span class="subst">$&#123;state === <span class="string">&#x27;1&#x27;</span> ? <span class="string">&#x27;active&#x27;</span> : <span class="string">&#x27;&#x27;</span>&#125;</span>`</span>&#125; onclick=&#123;<span class="function">() =&gt;</span> activeClick&#125;&gt;&lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="使用-typescript-创建函数组件和类组件"><a href="#使用-typescript-创建函数组件和类组件" class="headerlink" title="使用 typescript 创建函数组件和类组件"></a>使用 typescript 创建函数组件和类组件</h3><ul>
<li>创建函数组件</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建函数组件的快捷方式：输入 rafce 然后回车</span></span><br><span class="line"><span class="keyword">const</span> Home = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;div&gt;Home&lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"><span class="string">``</span><span class="string">``</span></span><br><span class="line"></span><br><span class="line">* 创建类组件</span><br><span class="line"></span><br><span class="line"><span class="string">``</span><span class="string">`js</span></span><br><span class="line"><span class="string">import React from &#x27;react&#x27;</span></span><br><span class="line"><span class="string">import store from &#x27;@/store/store&#x27;</span></span><br><span class="line"><span class="string">import &#x27;./main.css&#x27;</span></span><br><span class="line"><span class="string">import &#123; songList &#125; from &#x27;@/request/song&#x27;</span></span><br><span class="line"><span class="string">import &#123;</span></span><br><span class="line"><span class="string">    UserOutlined</span></span><br><span class="line"><span class="string">&#125; from &#x27;@ant-design/icons&#x27;;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// 声明 props 类型</span></span><br><span class="line"><span class="string">interface propsType &#123;</span></span><br><span class="line"><span class="string">    value: string</span></span><br><span class="line"><span class="string">    songList: Array&lt;Object&gt;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// 声明 songList 数组每个对象的类型</span></span><br><span class="line"><span class="string">interface songListObj &#123;</span></span><br><span class="line"><span class="string">    name: string</span></span><br><span class="line"><span class="string">    picUrl: string</span></span><br><span class="line"><span class="string">    id: number</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// 声明 state 的类型</span></span><br><span class="line"><span class="string">interface stateType &#123;</span></span><br><span class="line"><span class="string">    songList: Array&lt;songListObj&gt;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// 必须声明 props 和 state 的类型，否则会报错：&#x27;类型“Readonly&lt;&#123;&#125;&gt;”上不存在属性“songList”&#x27;</span></span><br><span class="line"><span class="string">class main extends React.Component&lt;propsType, stateType&gt; &#123;</span></span><br><span class="line"><span class="string">    constructor(props: propsType) &#123;</span></span><br><span class="line"><span class="string">        // 使用 this 之前一定要调用 super() 继承父组件的方法</span></span><br><span class="line"><span class="string">        super(props)</span></span><br><span class="line"><span class="string">        this.state = &#123;</span></span><br><span class="line"><span class="string">            songList: [&#123;id: 1, name: &#x27;李四&#x27;, picUrl: &#x27;https://baidu.com&#x27;&#125;]</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    render() &#123;</span></span><br><span class="line"><span class="string">        return (</span></span><br><span class="line"><span class="string">            &lt;div className=&quot;mian&quot;&gt;&#123;this.state.songList&#125;&lt;/div&gt;</span></span><br><span class="line"><span class="string">        )</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">export default main</span></span><br></pre></td></tr></table></figure>


<h3 id="函数组件和类组件获取-store-中的数据"><a href="#函数组件和类组件获取-store-中的数据" class="headerlink" title="函数组件和类组件获取 store 中的数据"></a>函数组件和类组件获取 store 中的数据</h3><ul>
<li>函数组件中获取 store 数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数组件: 使用 context 获取</span></span><br><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="comment">// 导入文件中所有 action 函数</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> allAction <span class="keyword">from</span> <span class="string">&#x27;@/store/action/layoutAction&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 将store中的数据同步到组件的props属性中</span></span><br><span class="line"><span class="keyword">const</span> mapState = <span class="function">(<span class="params">state: any</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="comment">// 获取 layoutReducer 函数中的数据</span></span><br><span class="line">        value: state.layoutReducer.value</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将 store 中的 action 映射到 props ，返回一个 action</span></span><br><span class="line"><span class="keyword">const</span> mapAction = <span class="function">(<span class="params">dispatch: Dispatch&lt;AnyAction&gt;</span>) =&gt;</span> bindActionCreators(allAction, dispatch)</span><br><span class="line"></span><br><span class="line">interface propsType &#123;</span><br><span class="line">    value: string</span><br><span class="line">    actionFn: <span class="built_in">Function</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Home = <span class="function">(<span class="params">props:propsType </span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        <span class="comment">// actionFn 是文件导出的 action 函数，用于修改 reducer 函数中的值</span></span><br><span class="line">        &lt;div onClick=&#123;<span class="function">() =&gt;</span> props.actionFn(<span class="string">&#x27;111111&#x27;</span>)&#125;&gt;&#123;props.value&#125;&lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(mapState, mapAction)(Home)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>类组件中获取 store 数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 类组件中获取 store 的值</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;@/store/store&#x27;</span></span><br><span class="line"></span><br><span class="line">interface propsType &#123;</span><br><span class="line">    value: string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface stateType &#123;</span><br><span class="line">    list: string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在类组件中，必须使用泛型的形式规定类组件中 props 和 state 的类型，否则在使用 this.state.xxxx 的时候会报错：类型“Readonly&lt;&#123;&#125;&gt;”上不存在属性“songList”</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">main</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span>&lt;<span class="title">propsType</span>, <span class="title">stateType</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props: propsType</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            list: <span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">// 通过 store 获取 redux 中的值</span></span><br><span class="line">        store.getState(); <span class="comment">// 获取状态</span></span><br><span class="line">        <span class="comment">// 订阅状态，当store中的状态发生改变的时候，会自动调用该方法，重新渲染组件（如果使用了Provider，则不需要使用订阅状态）</span></span><br><span class="line">        store.subscribe(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;)</span><br><span class="line">        <span class="comment">// 触发 action </span></span><br><span class="line">        store.dispatch(&#123; <span class="attr">type</span>: <span class="string">&#x27;layout&#x27;</span>, <span class="attr">payload</span>: <span class="string">&#x27;新的值&#x27;</span> &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div&gt;内容<span class="number">1</span>&lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> main</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="setState-更新数据后，console-log-不能及时打印更新后的内容"><a href="#setState-更新数据后，console-log-不能及时打印更新后的内容" class="headerlink" title="setState 更新数据后，console.log() 不能及时打印更新后的内容"></a>setState 更新数据后，console.log() 不能及时打印更新后的内容</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// setState 修改数据是一个异步操作，所以会导致打印的内容更新不及时</span></span><br><span class="line"><span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(props)</span><br><span class="line">    <span class="built_in">this</span>.state = &#123;</span><br><span class="line">        value: <span class="string">&#x27;1&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">this</span>.setState(&#123;</span><br><span class="line">    value: <span class="string">&#x27;2&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">this</span>.state.value) <span class="comment">// 1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 解决方法，使用 setState 的回调函数来解决异步问题</span></span><br><span class="line"><span class="built_in">this</span>.setState(&#123;</span><br><span class="line">    value: <span class="string">&#x27;2&#x27;</span></span><br><span class="line">&#125;, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.state.value) <span class="comment">// 2  打印出最新的结果了</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="reducer-中的数据出现-undefined"><a href="#reducer-中的数据出现-undefined" class="headerlink" title="reducer 中的数据出现 undefined"></a>reducer 中的数据出现 undefined</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// reducer.ts</span></span><br><span class="line"><span class="comment">// 原因：没有把 state 中的全部数据返回，只返回了对应 action 的数据所以导致其它数据为 undefined</span></span><br><span class="line">interface initStateType &#123;</span><br><span class="line">    value: string</span><br><span class="line">    alpha: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> initState: initStateType = &#123;</span><br><span class="line">    value: <span class="string">&#x27;华语男歌手&#x27;</span>,</span><br><span class="line">    alpha: -<span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> layoutReducer = <span class="function">(<span class="params">state = initState, action: any</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span> (action.type) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;layout&#x27;</span>:</span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">                <span class="comment">// 返回其它的 state 的值，防止其它的值为 undefined</span></span><br><span class="line">                ...state,</span><br><span class="line">                value: action.payload</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&#x27;alpha&#x27;</span>:</span><br><span class="line">                <span class="keyword">return</span> &#123;</span><br><span class="line">                    <span class="comment">// 返回其它的 state 的值，防止其它的值为 undefined</span></span><br><span class="line">                    ...state,</span><br><span class="line">                    alpha: action.payload</span><br><span class="line">                &#125;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> layoutReducer</span><br></pre></td></tr></table></figure>

<h3 id="ts-中使用-useRef-获取组件实例"><a href="#ts-中使用-useRef-获取组件实例" class="headerlink" title="ts 中使用 useRef() 获取组件实例"></a>ts 中使用 useRef() 获取组件实例</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Home.tsx</span></span><br><span class="line"><span class="keyword">import</span> &#123; useRef &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line">interface propsType &#123;&#125;</span><br><span class="line"><span class="comment">// 声明获取的实例中的类型</span></span><br><span class="line">interface domType &#123;</span><br><span class="line">    show: <span class="built_in">Function</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> Home = <span class="function">(<span class="params">props: propsType</span>) =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义泛型，声明使用到属性的类型</span></span><br><span class="line">    <span class="keyword">const</span> dom = useRef&lt;domType&gt;(<span class="literal">null</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> click = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(dom)</span><br><span class="line">        <span class="comment">// dom.current 可能存在空值的情况，所以需要加判断</span></span><br><span class="line">        <span class="comment">// 触发子组件的方法</span></span><br><span class="line">        dom.current &amp;&amp; dom.current.show()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">            &lt;div onClikc=&#123;click&#125;&gt;调用子组件的方法&lt;/div&gt;</span><br><span class="line">            &lt;child ref=&#123;dom&#125; /&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// child.tsx</span></span><br><span class="line">interface propsType &#123;</span><br><span class="line">    ref: <span class="built_in">Object</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> Child = <span class="function">(<span class="params">props: propsType</span>) =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> show = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        consoleo.log(<span class="string">&#x27;child&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;div&gt;child&lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="组件调用另一个组件的方法（个人推荐使用）"><a href="#组件调用另一个组件的方法（个人推荐使用）" class="headerlink" title="组件调用另一个组件的方法（个人推荐使用）"></a>组件调用另一个组件的方法（个人推荐使用）</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过向组件传递一个方法，在另一个组件中触发该方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 具有层级关系</span></span><br><span class="line"><span class="comment">// Home.tsx</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> dom = useRef(<span class="literal">null</span>) <span class="comment">// 这里使用的 useRed ，具体看上一个标题的内容</span></span><br><span class="line">cosnt homeClick = () &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;home的方法&#x27;</span>)</span><br><span class="line">    dom.current &amp;&amp; dom.current.about()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&lt;div&gt;</span><br><span class="line">    &lt;child func=&#123;homeClick&#125; /&gt;</span><br><span class="line">    &lt;about ref=&#123;aboutDom&#125; /&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// About.tsx</span></span><br><span class="line"><span class="keyword">const</span> about = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> about = () &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;child 组件通过触发 home 组件中的方法，进而触发 about 方法，从而实现了一个组件调用其它组件的方法&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Child.tsx</span></span><br><span class="line"></span><br><span class="line">interface propsType &#123;</span><br><span class="line">    func: <span class="built_in">Function</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> child = (props: propsType) &#123;</span><br><span class="line">    <span class="keyword">const</span> childClick = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 触发父组件传递过来的方法</span></span><br><span class="line">    props.func()</span><br><span class="line">&#125;</span><br><span class="line">    &lt;div onClick=&#123;childClick&#125;&gt;&lt;/div&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="React-FC-lt-gt-声明的组件即为函数组件，是-typescript-使用的一个泛型，class-xx-extends-React-Component-声明的组件则是类组件"><a href="#React-FC-lt-gt-声明的组件即为函数组件，是-typescript-使用的一个泛型，class-xx-extends-React-Component-声明的组件则是类组件" class="headerlink" title="React.FC&lt;&gt; 声明的组件即为函数组件，是 typescript 使用的一个泛型，class xx extends React.Component 声明的组件则是类组件"></a>React.FC&lt;&gt; 声明的组件即为函数组件，是 typescript 使用的一个泛型，class xx extends React.Component 声明的组件则是类组件</h3><ul>
<li>使用React.FC&lt;&gt; 声明函数组件和普通函数声明函数组件的好处是：不用显示的声明 props.children 的类型</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, &#123; FC &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line">interface Props &#123;</span><br><span class="line">    value: string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Home: FC&lt;Props&gt; = <span class="function">(<span class="params">&#123;value, children&#125;</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 直接使用 children</span></span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Home&#123;childred&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 普通函数声明函数组件</span></span><br><span class="line">interface Props &#123;</span><br><span class="line">    <span class="comment">// 需要手动声明 children 的类型</span></span><br><span class="line">    childred?: React.ReactNode</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Home = <span class="function">(<span class="params">Props</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Home&#123;children&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="使用场景，使用-props-children-将子组件的内容展示在父组件中"><a href="#使用场景，使用-props-children-将子组件的内容展示在父组件中" class="headerlink" title="使用场景，使用 props.children 将子组件的内容展示在父组件中"></a>使用场景，使用 props.children 将子组件的内容展示在父组件中</h2><ul>
<li><p>首先声明 children 的类型</p>
</li>
<li><p>props.children 展示子组件的内容</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Layout</span></span><br><span class="line"><span class="keyword">import</span> React, &#123; ReactChild &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line">interface propsType &#123;</span><br><span class="line">    children: ReactChild</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> Layout = <span class="function">(<span class="params">props: propsType</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;div&gt;&#123;props.children&#125;&lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// children</span></span><br><span class="line"><span class="keyword">import</span> Layout <span class="keyword">from</span> <span class="string">&#x27;Layout&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数组件</span></span><br><span class="line"><span class="keyword">const</span> children = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;Layout&gt;children&lt;/Layout&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类组件</span></span><br><span class="line">interface PropsType &#123;&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">children2</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span>&lt;<span class="title">PropsType</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props: PropsType</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">    &#125;</span><br><span class="line">    render () &#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;Layout&gt;children2&lt;/Layout&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <div class="article-footer">
      <!-- <blockquote class="mt-2x"> -->
  <!-- <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/Yu-Luoboke/2021/12/14/react%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/" title="react开发日志" target="_blank" rel="external">https://gitee.com/Yu-Luoboke/2021/12/14/react%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul> -->
<!-- </blockquote> -->


<!-- <div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="http://yu-luoboke.gitee.io/blog" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/blog/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="http://yu-luoboke.gitee.io/blog" target="_blank"><span class="text-dark">雨落</span><small class="ml-1x">前端开发-奋斗青年</small></a></h3>
        <div>前端经验总结。</div>
      </div>
    </figure>
  </div>
</div> -->


    </div>
  </article>
  
    

  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/blog/2021/12/27/vue%E5%B7%A5%E4%BD%9C%E5%BD%92%E7%BA%B3/" title="vue工作归纳"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/blog/2021/12/03/%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A1%A8%E6%83%85%E5%8C%85/" title="自定义表情包"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  
  <div class="bar-right">
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="http://yu-luoboke.gitee.io/blog" target="_blank" title="Github" ><i class="icon icon-github"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        &copy; 2024 lianghongming
        
        <div class="publishby">
        	Theme by <a href="https://gitee.com/Yu-Luoboke/blog" target="_blank"> blog </a>base on <a href="https://gitee.com/Yu-Luoboke/blog" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/blog/js/plugin.min.js"></script>


<script src="/blog/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/blog/',
        CONTENT_URL: '/blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/blog/js/insight.js"></script>






   









</body>
</html>